package com.rongshu.view.home.presenter;

import android.content.Context;
import android.content.Intent;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.home.contract.PenPalView;
import com.rongshu.view.home.entity.PenpalHomeResponse;
import com.rongshu.view.penpal.activity.PenpalLabelActivity;
import com.rongshu.view.penpal.activity.PenpalSearchActivity;
import com.rongshu.view.penpal.bean.PenpalRecommendArticleResponse;
import com.rongshu.widget.BanyanDialog;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Action0;

/**
 * created by zhengweis on 2018/11/21.
 * description：
 */
public class PenpalPresenter extends BasePresenter<PenPalView> {

    public PenpalPresenter(Context context) {
        super(context);
    }

    public void getRecommendPenpalList() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        RetorfitService.getPenpalHome(UserInfoUtils.getUserId(),
                Common.encryptionSign(map))
                .compose(getView().<PenpalHomeResponse>bindToLife()).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<PenpalHomeResponse>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(PenpalHomeResponse object) {
                getView().callBackPenpalList(object);
            }
        });
    }

    public void getAriticleList(int pageIndex, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        RetorfitService.getPenpalRecommendArticles(UserInfoUtils.getUserId(), pageIndex, pageSize,
                Common.encryptionSign(map))
                .compose(getView().<PenpalRecommendArticleResponse>bindToLife()).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<PenpalRecommendArticleResponse>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(PenpalRecommendArticleResponse object) {
                getView().callBackArticles(object);
            }
        });
    }

    public void toSearchPenpal() {
        Intent intent = new Intent(mContext, PenpalSearchActivity.class);
        mContext.startActivity(intent);
    }

    public void createPenpalPermission() {
        ((BaseActivity)mContext).showLoading(true);
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        RetorfitService.createPenpalPermission(UserInfoUtils.getUserId(),
                Common.encryptionSign(map))
                .compose(getView().<INetBean>bindToLife()).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<INetBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                ((BaseActivity)mContext).showLoading(false);
            }

            @Override
            public void onNext(INetBean object) {
                ((BaseActivity)mContext).showLoading(false);
                if (object.getErrCode() == 0) {
                    Intent intent = new Intent(mContext, PenpalLabelActivity.class);
                    mContext.startActivity(intent);
                } else {
                    BanyanDialog.Builder builder = new BanyanDialog.Builder(mContext);
                    builder.setTitle("你已是会长，无法再次创建");
                    builder.setRight("我知道了");
                    final BanyanDialog dialog = builder.create();
                    builder.setDialogOnClick(new BanyanDialog.Builder.onClickListener() {
                        @Override
                        public void leftClickListener() {

                        }

                        @Override
                        public void rightClickListener() {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                }
            }
        });
    }
}
